package com.baomidou.springwind.service.impl;

import org.springframework.stereotype.Service;

import com.baomidou.framework.annotations.Log;
import com.baomidou.springwind.entity.User;
import com.baomidou.springwind.entity.UserRole;
import com.baomidou.springwind.mapper.UserRoleMapper;
import com.baomidou.springwind.service.IUserRoleService;
import com.baomidou.springwind.service.support.BaseServiceImpl;

/**
 *
 * UserRole 表数据服务层接口实现类
 *
 */
@Service
public class UserRoleServiceImpl extends BaseServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

	@Override
	public boolean existRoleUser( Long roleId ) {
		UserRole ur = new UserRole();
		ur.setRid(roleId);
		int rlt = baseMapper.selectCount(ur);
		return rlt >= 1;
	}

	@Log("更新用户关联角色")
	@Override
	public boolean updateRole4User(User user, Long rid) {
		try {
			UserRole record = new UserRole();
			record.setUid(user.getId());
			record = baseMapper.selectOne(record);
			if(record != null){
				record.setRid(rid);
				baseMapper.updateSelectiveById(record);
			}else{
				record = new UserRole();
				record.setRid(rid);
				record.setUid(user.getId());
				baseMapper.insert(record);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}